/**
 * Created by Administrator on 15-6-26.
 */
var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		oData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有数据...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	},
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
	},
	setCurPage : function(page){
		this.statics.currentPage = page;
	},
	getCurPage : function(){
		return this.statics.currentPage;
	},
	getData : function(){
		return this.statics.oData;
	},
	setData : function(id,data){
		if(id && data){
			this.statics.oData[id] = data;
		}else{
			this.statics.oData = {};
		}
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if(tarBtn.hasClass("disable")) return false;
		var oData = that.statics.oData;
		var curPage = that.statics.currentPage;
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		if(tarBtn.hasClass("prevBtn")){
			that.nextBtn.removeClass("disable");
			that.statics.currentPage = toPage;
			//console.log(oData[toPage]);
			that.buildHtml(oData[toPage]);
			if(!that.statics.oData[toPage-1]) that.prevBtn.addClass("disable");
		}else{
			
			that.prevBtn.removeClass("disable");
			if(oData[toPage]){
				that.statics.currentPage = toPage;
				that.buildHtml(oData[toPage]);
				if(!that.statics.oData[toPage+1] && !that.statics.last){
					that.nextBtn.addClass("disable");
				}
			}else{
				that.fire("query_nexta",oData[curPage]["last"]);
			}
		}
	},
	query : function(opt){
		var that = this;
		var opt = opt || {};
		var pageSize = opt.pageSize || 5;
		var last = opt.last || "";
		var role = opt.role || "";
		var dname = opt.dname || "";
		var dstatus = opt.dstatus || "-1";
		var currentPage = opt.currentPage || "0";
		var type = opt.type || "0";
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data = {
			pageSize : pageSize,
			last : last,
			role : role,
			dname : dname,
			dstatus : dstatus,
			currentPage : last,
			set_page : "1"
		};
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		PFT.Ajax({
			url : "http://www.12301.cc/alliance_mem.html?lists=get",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage += 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);} 
		},function(res){
			var lists = res.list;
			var last = res.total;
			var totalPage = res.totalPage;
			if(type=="1"){
				$("#tab_icon").attr("data-type","1");
				that.statics.oData[that.statics.currentPage] = null;
			}
				if(!that.statics.isObjEmpty(lists)){
					that.statics.oData[that.statics.currentPage] = res;
					//that.statics.oData[that.statics.totalPage] = res;
					//that.statics.oData[that.statics.totalPage] = res;
					that.buildHtml(res);
				}else{
					listUl.html(status["empty"]);
					$("#pagenavW").hide();
				}
				that.statics.last = last;
				if(!last){
					that.fire("no_next",res);
				}else{
					that.fire("has_next",res);
				}
		})
	},
	buildHtml : function(res){
		var that = this;
		//var code = res.code;
		var lists = res.list;
		var str = "";
		var last = res.last;
		var data = lists;
		var total = res.total;
		var totalPage = res.totalPage || "0";
		
		if(totalPage==1){
			$("#pagenavW").hide();
		}
		    str +=     '<thead >';
            str +=    ' <tr>';
            str +=        '<th class="setname">优惠券名称</th>';
            str +=        '<th class="setmoney">面额</th> ';
            str +=        '<th class="settype">发行方式</th>';
            str +=        '<th class="setAlign">使用渠道</th>';
            str +=       '<th class="settime">有限期限</th>';
            str +=        '<th class="setdo">操作</th>';
            str +=      '</tr>';
            str +=    '</thead>';
		for(var i in data){
			is_data_empty = false;
			str += 		'<tbody class="tbody" id="tab">';
            str += 		'      <tr class="setbg_a">';
            str += 		'        <td class="setname">'+data[i]["dname"]+'</td>';
            str += 		'        <td class="setmoney">'+data[i]["role"]+'</td>';
            str += 		'        <td class="settype">卖家派发</td>';
            str += 		'        <td class="setAlign">'+data[i]["deposit"]+'</td>';
            str += 		'        <td class="settime">2015/9/12至2015/9/15</td>';
            str += 		'       <td class="setdo">';
			str += 		'			<span class="edit">编辑</span>';
			str += 		'			<span class="user">适用产品</span>';
			str += 		'			<span class="console_l">领券情况</span>';
			str += 		'			<span class="del">作废</span>';
			str += 		'		</td>';
            str += 		'      </tr>';
			str += 		'</tbody>';
			
			
		}
		$("#mlistUl").html(str);
		$("#whichPageNum").text(that.statics.currentPage);
		 $(".time").each(function(){
			var active = $(this).html();
			if(active=="2"){
				$(this).html("申请中");
			}
			if(active=="0"){
				$(this).html("正常(已审核)");
			}
			if(active=="1"){
				$(this).html("退出");
			}
			if(active=="3"){
				$(this).html("驳回");
			}
			
		});
		$(".edit").each(function(){
			var active = $(this).attr("data-active");
            var pledge_status = $(this).find(".stc").attr("ple_sta");
			if(active=="2"){
				$(this).find(".sta").html("驳回&nbsp;&nbsp;");
				$(this).find(".stb").html("通过");
			}

			if(active=="0"){
				$(this).find(".sta").html("退出");
			}
            if(pledge_status == '1'){
                if(active=="1"){
                    $(this).find(".stc").html("退押金");
                }

                if(active=="3"){
                    $(this).find(".stc").html("退押金");
                }
            }
			
			
			
		})
	}
	
});





